package com.example.mynotepad.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.mynotepad.utils.MyConstant;
import com.example.mynotepad.bean.RecordBean;


import java.util.ArrayList;
import java.util.List;

public class MyOpenHelper extends SQLiteOpenHelper {

    // 创建数据表的sql语句
    public static final String CREATE_TABLE = "create table "+ MyConstant.TABLE_NAME
            +"(" + MyConstant.CLOME_ID +" integer primary key autoincrement,"
            + MyConstant.CLOME_RECORD + " text,"
            + MyConstant.CLOME_TIME + " text)";

    // 创建数据表对象
    private SQLiteDatabase db;

    // 构造方法
    public MyOpenHelper(@Nullable Context context) {
        super(context, MyConstant.DB_NAME, null, 1); // 创建数据库
        db = this.getWritableDatabase(); // 创建可读写数据表
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        // 创建数据表
        sqLiteDatabase.execSQL(CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }

    /**
     * 向数据库添加记录
     * @param record  记录内容
     * @param time  保存事件
     * @return  添加记录是否成功的状态值
     */
    public long insertRecord(String record, String time){
        ContentValues values = new ContentValues();
        values.put(MyConstant.CLOME_RECORD, record);
        values.put(MyConstant.CLOME_TIME, time);
        return db.insert(MyConstant.TABLE_NAME, null, values);
    }

    /**
     * 修改数据库中指定id的记录
     * @param id
     * @param record
     * @param time
     * @return  修改了几条记录
     */
    public int updateRecord(String id, String record, String time){
        ContentValues values = new ContentValues();
        values.put(MyConstant.CLOME_RECORD, record);
        values.put(MyConstant.CLOME_TIME, time);
        return db.update(MyConstant.TABLE_NAME, values, "id=?", new String[]{id});
    }

    /**
     * 删除数据库中指定id的记录
     * @param id
     * @return  删除了几条记录
     */
    public int deleteRecord(String id){
        return db.delete(MyConstant.TABLE_NAME, "id=?", new String[]{id});
    }

    /**
     * 查询全部记录
     * @return  查询到的所有数据存入该集合
     */
    public List<RecordBean> queryRecord(){
        Cursor cursor = db.query(MyConstant.TABLE_NAME,null,null,null,
                null,null,MyConstant.CLOME_TIME +" desc");
        List<RecordBean> recordBeanList = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                // 使用字段名先获取字段编号，再通过字段编号获取字段值。
                // 其中获得的id是int型，需转换。
                String id = String.valueOf(cursor.getInt(cursor.getColumnIndex(MyConstant.CLOME_ID)));
                String record = cursor.getString(cursor.getColumnIndex(MyConstant.CLOME_RECORD));
                String time = cursor.getString(cursor.getColumnIndex(MyConstant.CLOME_TIME));
                RecordBean recordBean = new RecordBean(id, record, time);
                recordBeanList.add(recordBean);
            }
            cursor.close(); // 游标需要关闭
        }
        return recordBeanList;
    }

}
